package com.gps.skyrc.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gps.skyrc.R;
import com.gps.skyrc.activity.base.BaseActivity;
import com.gps.skyrc.tool.MyTitleBar;
import com.gps.skyrc.tool.SharedPreferencesUtils;

import butterknife.BindView;

/**
 * Create by ywq
 * data 2018/11/24
 */
public class UnitActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.unit_title)
    MyTitleBar mUnitTitle;
    @BindView(R.id.km_rl)
    RelativeLayout mKmRl;
    @BindView(R.id.mile_rl)
    RelativeLayout mMileRl;
    @BindView(R.id.km_choose_iv)
    ImageView mKmChooseIv;
    @BindView(R.id.mile_choose_iv)
    ImageView mMileChooseIv;

    private int choose = 0;//0默认千米  1英里
    private String mLastMac;
    private int mUnit;

    @Override
    protected void getLayout() {
        setContentView(R.layout.activity_unit);
    }

    @Override
    protected void setTitleBar() {
        mUnitTitle.initViewsVisible(true,true,false,true,false,true);
        mUnitTitle.setOnLeftButtonClickListener(v -> {
            SharedPreferencesUtils.setParam(mContext,mLastMac,choose);
            onBackPressed();
        });
        mUnitTitle.setAppTitle(getResources().getString(R.string.unit_switch));
    }

    @Override
    protected void init() {
        mLastMac = (String) SharedPreferencesUtils.getParam(mContext, SharedPreferencesUtils.PreKey.LAST_DEVICE_MAC, "");
        mUnit = (int) SharedPreferencesUtils.getParam(mContext,mLastMac, 0);//根据MAC地址作为key,单位为value
        if (mUnit== 0)
            choose = 0;
        else {
            choose = 1;
        }
        if (choose == 0)
            chooseSwitch(true);
        else
            chooseSwitch(false);
        mKmRl.setOnClickListener(this);
        mMileRl.setOnClickListener(this);
    }

    private void chooseSwitch(boolean isKm){
        mKmChooseIv.setVisibility(isKm ? View.VISIBLE : View.INVISIBLE);
        mMileChooseIv.setVisibility(isKm ? View.INVISIBLE : View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.km_rl:
                chooseSwitch(true);
                choose = 0;
                break;
            case R.id.mile_rl:
                chooseSwitch(false);
                choose = 1;
                break;
        }
    }
}
